package com.pengcheng.taskapplication.mvp.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.BankListModel;
import com.pengcheng.taskapplication.model.CarSignModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenterV2;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;

import java.lang.reflect.Type;

import butterknife.BindView;
import butterknife.OnClick;

public class CarSignAndUnSign extends BaseActivity implements IView {

    @BindView(R.id.tv_center_title)
    TextView tv_center_title;

    @BindView(R.id.ll_aggress)
    LinearLayout ll_aggress;

    @BindView(R.id.tv_sign_tip)
    TextView tv_sign_tip;

    @BindView(R.id.tv_car_num)
    TextView tv_car_num;
    @BindView(R.id.tv_e_car_num)
    TextView tv_e_car_num;

    @BindView(R.id.ck_select)
    CheckBox ck_select;

    UserPresenterV2 userPresenterV2;

    private   CarSignModel carSignModel;

    private String uid;
    private String newToken;

    @Override
    protected int layoutResID() {
        return R.layout.activity_car_sign_unsign;
    }

    @Override
    protected void initData() {
        userPresenterV2=new UserPresenterV2(this);
        uid= SPUtils.getString(context,ConstantValue.SpType.userid);
        newToken=SPUtils.getString(context,ConstantValue.SpType.newtoken);
        carSignModel=(CarSignModel)getIntent().getSerializableExtra("model");
       if(carSignModel!=null){
           if(carSignModel.isSign.equals("1")){
               tv_center_title.setText("解除签约");
               ll_aggress.setVisibility(View.INVISIBLE);
               tv_sign_tip.setText("解除签约");
           }else{
               tv_center_title.setText("签约");
               ll_aggress.setVisibility(View.VISIBLE);
               tv_sign_tip.setText("签约");
           }
           tv_car_num.setText(carSignModel.getPlate_no());
           tv_e_car_num.setText(carSignModel.getEcard_code());
       }

    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.getBankList)){
            Gson gson= new Gson();
            Type type =new TypeToken<BankListModel>(){}.getType();
            BankListModel bankListModel= gson.fromJson(gson.toJson(data),type);
            if(bankListModel.getList().size()==0){
                //没有绑卡
                Intent intent =new Intent(context,AddBankCard.class);
                startActivity(intent);
            }else {
                //已经帮过卡了
                //进行签约
                signCarNumber(newToken,uid,carSignModel.id,"1");
            }
        }

        if(actionType.equals(ConstantValue.RequestKey.signCarNumber)){
           Intent intent =new Intent(context,SignSuccess.class);
           startActivity(intent);
           context.finish();
        }

        if(actionType.equals(ConstantValue.RequestKey.unsignCarNumber)){
//            Intent intent =new Intent(context,SignSuccess.class);
//            startActivity(intent);
            ToastUtils.showShortToast("解约成功");
            context.finish();
        }
    }

    @Override
    public void onFailed(String code, String msg) {

    }

    @OnClick({R.id.img_back,R.id.ll_sign,R.id.tv_agress})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_agress:



                break;
            //签约和解除签约
            case R.id.ll_sign:
                if(carSignModel!=null){

                    if(carSignModel.isSign.equals("1")){
                       //调用解除签约
                        unsignCarNumber(newToken,uid,carSignModel.id,"2");
                    }else{
                        //调用签约,判断是否勾选
                        if(!ck_select.isChecked()){
                            ToastUtils.showShortToast("请勾选同意无感支付协议");
                            return;
                        }


                        //判断是否签约，，是否有过绑卡列表

                        getBankList(newToken,uid);



//                        //调用签约接口,是否具备签约准备工作，是否帮过卡，等等，，这里先测试，走到绑卡页面
//                        Intent intent =new Intent(context,AddBankCard.class);
//                        startActivity(intent);
                    }


                }

                break;

        }
    }


    public void getBankList(String newtoken,String uid){
        userPresenterV2.getBankList(newtoken,uid, ConstantValue.RequestKey.getBankList);
    }

    public void signCarNumber(String newtoken,String uid,String mvid,String signtype){
        userPresenterV2.signCarNumber(newtoken,uid,mvid,signtype, ConstantValue.RequestKey.signCarNumber);
    }

    public void unsignCarNumber(String newtoken,String uid,String mvid,String signtype){
        userPresenterV2.signCarNumber(newtoken,uid,mvid,signtype, ConstantValue.RequestKey.unsignCarNumber);
    }

}
